﻿using System;
using System.Collections.Generic; 
using System.ComponentModel;
using System.Globalization;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Config;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Date
{
    [Serializable]
    [VerfityAttribute(Id = "VerfityRq0222", Name = "(非)未来日期隔检证", CategoryType = EnumCategoryType.日期, Model = true)]
    public class VerfityRq0222 : VerfityOneResult
    {
        private bool _futureDate = true;
        
        [Category("是否未来日期"), Description("true:检查未来日期 false:检查非未来日期")]
        public bool FutureDate
        {
            get { return _futureDate; }
            set { _futureDate = value; }
        }  

        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;

            //CultureInfo ci = new CultureInfo("ja-JP");
            //Calendar cal = new JapaneseCalendar();
            //ci.DateTimeFormat.Calendar = cal;
            //return DateTime.ParseExact(entryChangeDateStr, DateResultFormat, ci); 

            foreach (var rowResult in result)
            {
                //公历 -> 和历
                //var changeRowResult = new DateHelper(ChildImageObject.CategorySetting).ChangeDateStr(rowResult);

                foreach (var colResult in rowResult)
                {
                   
                        var completeResult =  CompleteDate(colResult);
                        var splitEntryResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, completeResult);

                        //每个结果的录入日期
                        string entryDate = splitEntryResult.GetDate();
                        string dateFormat = splitEntryResult.DateResultFormat; 

                        // 本次结果是 empty
                        // 继续比较下一个
                        if (EmptyResultCount(entryDate) > 0 || QuestionMarkResultCount(entryDate) > 0)
                             continue;

                        DateTime? dateTimeResult = splitEntryResult.GetDateTime();
   
                        List<string> arrayDateFormat = dateFormat.Split(new char[] { '/' }).ToList();
                            
                        if (dateTimeResult != null)
                        {

                            int indexYy = arrayDateFormat.IndexOf("yy");
                            if (indexYy == -1)
                            {
                                indexYy = arrayDateFormat.IndexOf("yyyy");
                            }

                            //去年数据 今年检验
                            //结果中没有年
                            //补充完成的时间 年份-1
                            if (DateTime.Now.Month.Equals(1) &&
                                dateTimeResult.Value.Month > 10 &&
                                dateTimeResult.Value.Year.Equals(DateTime.Now.Year) &&
                                indexYy.Equals(-1)
                                )
                            {
                                dateTimeResult = dateTimeResult.Value.AddYears(-1);
                            }

                            //比较年月日 完整日期
                            bresult = CompareDate((DateTime)dateTimeResult);
                        }
                        else
                        {
                            //比较非完整日期
                         
                            int indexGannNenn = arrayDateFormat.IndexOf("gg");
                            int indexYy = arrayDateFormat.IndexOf("yy");
                            if (indexYy == -1)
                            {
                                indexYy = arrayDateFormat.IndexOf("yyyy");
                            }


                            string entryYear = string.Empty;
                            string entryGg = string.Empty;

                            //日期格式中有 年号 年
                            if (indexGannNenn > -1 && indexYy > -1)
                            {
                                //
                                entryYear = entryDate.Split(new char[] { '/' })[indexYy];
                                entryGg = entryDate.Split(new char[] { '/' })[indexGannNenn];

                                //如果是4位年
                                if (entryYear.Length == 4)
                                {
                                    bresult = CompareYear(entryYear);
                                }
                                else if (entryGg.Length == 2 && entryGg.Length > 0)
                                {
                                    //如果是2位年
                                    string entry = string.Format("{0}/{1}", entryGg, entryYear);

                                    //比较 年号/年
                                    bresult = CompareGgYear(entry);
                                }

                            }
                            else if (indexGannNenn == -1 && indexYy > -1)
                            {
                                //没有年号  只有年
                                entryYear = entryDate.Split(new char[] { '/' })[indexYy];
                                //4位年
                                if (entryYear.Length == 4)
                                {
                                    bresult = CompareYear(entryYear);
                                }
                            }
                        }

                        
                     

                    if (bresult)
                        break;
                    
                }

                if (bresult)
                    break;
            }

            if (bresult)
            {
                
                System.Console.WriteLine("VerfityRq0222");
            }

            return bresult;
        }


        /// <summary>
        /// 比较时间
        /// </summary>
        /// <param name="entryResult"></param>
        /// <returns></returns>
        private bool CompareDate(DateTime entryResult)
        {
            bool bresult = false;

            Nullable<DateTime> orderDate =   GetOrderDate();

            if (orderDate != null)
            {
                int compareYear = DateTime.Compare(entryResult, (DateTime)orderDate);

                if (_futureDate)
                {
                    if (compareYear > 0)
                    {
                        bresult = true;
                    }
                }
                else
                {
                    if (compareYear < 0)
                    {
                        bresult = true;
                    }
                }
            }
            return bresult;
        }

        /// <summary>
        /// 比较年
        /// </summary>
        /// <param name="entryYear"></param>
        /// <returns></returns>
        private bool CompareYear(string entryYear)
        {
            bool bresult = false;

            Nullable<DateTime> orderDate = GetOrderDate();

            if (orderDate != null)
            {
                string orderYear = ((DateTime) orderDate).Year.ToString(CultureInfo.InvariantCulture);
              
                int compareYear = System.String.Compare(entryYear, orderYear);

                if (_futureDate)
                {
                    if (compareYear > 0)
                    {
                        bresult = true;
                    }
                }
                else
                {
                    if (compareYear < 0)
                    {
                        bresult = true;
                    }
                }
            }
            return bresult;
        }

        /// <summary>
        /// 年号/年
        /// </summary>
        /// <param name="yearA"></param>
        /// <returns></returns>
        private bool CompareGgYear(string yearA )
        {
            bool bresult = false;

            string now = GetCurrentGannEnn();

            int compareYear = string.Compare(yearA, now);

            if (_futureDate)
            {
                if (compareYear > 0)
                {
                    bresult = true;
                }
            }
            else
            {
                if (compareYear < 0)
                {
                    bresult = true;
                }
            }
            return bresult;
        }

        private string GetCurrentGannEnn()
        {
            Nullable<DateTime> orderDate = GetOrderDate();


            if (orderDate != null)
            {

                CultureInfo ci = new CultureInfo("ja-JP");
                Calendar cal = new JapaneseCalendar();
                ci.DateTimeFormat.Calendar = cal;

                DateTime dt = (DateTime) orderDate;

                string gg = cal.GetEra(dt).ToString(CultureInfo.InvariantCulture);
                string yy=  dt.ToString("yy", ci);

                return string.Format("{0}/{1}", gg, yy);
            }
            else
            {
                return string.Empty;
            }
        }
    }
}
